// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * A handler for publishing a new blog post.
 */

goog.provide('rb.handlers.PublishHandler');

goog.require('rb.publish.Publisher');



/**
 * A handler that sets up the publishing system.
 *
 * @param {rb.ServiceProvider} sp The app's service provider.
 *
 * @constructor
 * @implements {relief.nav.Handler}
 */
rb.handlers.PublishHandler = function(sp) {
  this.sp_ = sp;
};


/**
 * A user must be an admin in order to publish new blog posts.
 */
rb.handlers.PublishHandler.requiredAuth = rb.auth.AuthLevel.ADMIN;


/**
 * Handle requests for /#/admin/post by displaying a UI for publishing new
 * blog posts.
 *
 * @param {relief.nav.Path} path The path.
 */
rb.handlers.PublishHandler.prototype.handle = function(path) {
  var publisher;
  /**
   * @type {rb.publish.Publisher}
   * @private
   */
  this.publisher_ = publisher = new rb.publish.Publisher(this.sp_);
  publisher.createNew();
};


/**
 * @inheritDoc
 */
rb.handlers.PublishHandler.prototype.transition = function(path, onTransition) {
  if (! this.publisher_) {
    this.handle(path);
  }
  onTransition(true);
};


/**
 * @inheritDoc
 */
rb.handlers.PublishHandler.prototype.exit = function(onExit, forced) {
  this.publisher_.tearDown();
  onExit(true);
};


/**
 * @inheritDoc
 */
rb.handlers.PublishHandler.prototype.dispose = function() {
  this.publisher_.dispose();
  this.publisher_ = this.sp_ = null;
};


/**
 * @inheritDoc
 */
rb.handlers.PublishHandler.prototype.isDisposed = function() {
  return this.sp_ === null;
};
